package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import com.greensock.*;

    public class ProjImageShell extends Sprite
    {
        var btnField:TextField;
        public var myXMLList:XMLList;
        var loader:Loader;
        var thebar:Sprite;
        var loaderbar:LoaderBar;
        var myURL:String;
        var fadeSpeed:Number;
        var closeBtn:Sprite;
        var bg:Sprite;
        var image:Bitmap;
        public var projId:int;
        var fsBtn:Sprite;
        var minus:Sprite;
        var frame:Frame;
        var fieldFormat:TextFormat;
        var plus:Sprite;
        var myRoot:MovieClip;
        var rArrow:Sprite;
        var currId:int;
        var lArrow:Sprite;

        public function ProjImageShell(param1:MovieClip, param2:Number, param3:XMLList, param4:int) : void
        {
            visible = false;
            alpha = 0;
            myRoot = param1;
            fadeSpeed = param2;
            myXMLList = param3;
            projId = param4;
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            bg = new Sprite();
            bg.buttonMode = true;
            addChild(bg);
            bg.graphics.beginFill(16777215, 100);
            bg.graphics.drawRect(0, 0, 1, 1);
            bg.graphics.endFill();
            bg.addEventListener(MouseEvent.CLICK, closeClick, false, 0, true);
            bg.addEventListener(MouseEvent.MOUSE_OVER, closeRollOver);
            bg.addEventListener(MouseEvent.MOUSE_OUT, closeRollOut);
            addChild(bg);
            buildArrows();
            loaderbar = new LoaderBar(myRoot, Math.round(4 * myRoot.unitW + 3 * myRoot.gutterW), 1, 15592941, 16724736);
            loaderbar.alpha = 0;
            addChild(loaderbar);
            return;
        }

        function closeRollOver(event:MouseEvent) : void
        {
            return;
        }

        public function loadImage(param1:int)
        {
            currId = param1;
            if (image != null)
            {
                image.smoothing = false;
            }
            loaderbar.progressSprite.width = 0;
            TweenMax.to(loaderbar, fadeSpeed, {alpha:1, overwrite:true, delay:fadeSpeed});
            loader = new Loader();
            loader.load(new URLRequest(myRoot.path + myXMLList[param1].@url));
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
            if (myRoot.localImagesXML.image.@id.contains(myXMLList[param1].@id))
            {
                plus.visible = false;
                minus.visible = true;
            }
            else
            {
                plus.visible = true;
                minus.visible = false;
            }
            return;
        }

        public function fadeOutShell() : void
        {
            if (frame != null)
            {
                TweenMax.to(frame, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                if (frame != null)
                {
                    removeChild(frame);
                    frame = null;
                }
                return;
            }
            });
            }
            TweenMax.to(image, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                killImage();
                return;
            }
            });
            TweenMax.to(thebar, fadeSpeed, {alpha:0, overwrite:true});
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, delay:fadeSpeed, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        function arrowRollOver(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            if (event.currentTarget.name == "closeBtn")
            {
                btnField.text = "Close";
                btnField.setTextFormat(fieldFormat);
            }
            else if (event.currentTarget.name == "fsBtn")
            {
                if (plus.visible == false)
                {
                    btnField.text = "Remove Bookmark";
                }
                else
                {
                    btnField.text = "Bookmark Image";
                }
                btnField.setTextFormat(fieldFormat);
            }
            else if (event.currentTarget.name == "lArrow")
            {
                btnField.text = "Previous";
                btnField.setTextFormat(fieldFormat);
            }
            else if (event.currentTarget.name == "rArrow")
            {
                btnField.text = "Next";
                btnField.setTextFormat(fieldFormat);
            }
            return;
        }

        function doResize() : void
        {
            if (loaderbar != null)
            {
                loaderbar.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
                loaderbar.y = Math.round(myRoot.gridCurrH + 13);
                loaderbar.width = Math.round(2 * myRoot.unitW + 1 * myRoot.gutterW);
                loaderbar.height = 1;
            }
            bg.width = myRoot.gridCurrW + 3;
            bg.height = myRoot.gridCurrH + 3;
            if (image != null)
            {
                var _loc_1:* = myRoot.unitW / 75;
                image.scaleY = myRoot.unitW / 75;
                image.scaleX = _loc_1;
                image.width = Math.round(image.width);
                image.height = Math.round(image.height);
            }
            thebar.x = 0;
            thebar.y = Math.round(bg.height + 7 * myRoot.unitW / 75);
            closeBtn.x = 0;
            fsBtn.x = 19;
            lArrow.x = 39;
            rArrow.x = 54;
            if (myXMLList.length() > 1)
            {
                btnField.x = 65;
            }
            else
            {
                btnField.x = 37;
            }
            var _loc_1:* = myRoot.unitW / 75;
            btnField.scaleY = myRoot.unitW / 75;
            btnField.scaleX = _loc_1;
            btnField.y = Math.round(-6 * myRoot.unitW / 75);
            if (frame != null)
            {
                frame.resizeFrame(image.width, image.height);
            }
            return;
        }

        function arrowRollOut(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            btnField.text = "";
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function progressHandler(event:ProgressEvent) : void
        {
            var _loc_2:* = undefined;
            _loc_2 = Math.round(event.bytesLoaded / event.bytesTotal * loaderbar.bgSprite.width);
            loaderbar.tweenTo(_loc_2, fadeSpeed);
            return;
        }

        function closeClick(event:MouseEvent) : void
        {
            dispatchEvent(new ClickCloseEvent());
            return;
        }

        function buildArrows() : void
        {
            thebar = new Sprite();
            addChild(thebar);
            closeBtn = new Sprite();
            closeBtn.graphics.beginFill(10066329, 0);
            closeBtn.graphics.drawRect(-10, -10, 24, 30);
            closeBtn.graphics.endFill();
            closeBtn.graphics.beginFill(myRoot.coldColor, 100);
            closeBtn.graphics.drawRect(1, 0, 1, 1);
            closeBtn.graphics.drawRect(7, 0, 1, 1);
            closeBtn.graphics.drawRect(0, 1, 3, 1);
            closeBtn.graphics.drawRect(6, 1, 3, 1);
            closeBtn.graphics.drawRect(1, 2, 3, 1);
            closeBtn.graphics.drawRect(5, 2, 3, 1);
            closeBtn.graphics.drawRect(2, 3, 5, 1);
            closeBtn.graphics.drawRect(3, 4, 3, 1);
            closeBtn.graphics.drawRect(2, 5, 5, 1);
            closeBtn.graphics.drawRect(1, 6, 3, 1);
            closeBtn.graphics.drawRect(5, 6, 3, 1);
            closeBtn.graphics.drawRect(0, 7, 3, 1);
            closeBtn.graphics.drawRect(6, 7, 3, 1);
            closeBtn.graphics.drawRect(1, 8, 1, 1);
            closeBtn.graphics.drawRect(7, 8, 1, 1);
            closeBtn.graphics.endFill();
            closeBtn.buttonMode = true;
            closeBtn.name = "closeBtn";
            closeBtn.addEventListener(MouseEvent.MOUSE_OVER, arrowRollOver);
            closeBtn.addEventListener(MouseEvent.MOUSE_OUT, arrowRollOut);
            closeBtn.addEventListener(MouseEvent.CLICK, arrowClick);
            thebar.addChild(closeBtn);
            fsBtn = new Sprite();
            fsBtn.graphics.beginFill(10066329, 0);
            fsBtn.graphics.drawRect(-5, -10, 19, 30);
            fsBtn.graphics.endFill();
            fsBtn.buttonMode = true;
            fsBtn.name = "fsBtn";
            fsBtn.addEventListener(MouseEvent.MOUSE_OVER, arrowRollOver);
            fsBtn.addEventListener(MouseEvent.MOUSE_OUT, arrowRollOut);
            fsBtn.addEventListener(MouseEvent.CLICK, arrowClick);
            thebar.addChild(fsBtn);
            plus = new Sprite();
            plus.graphics.beginFill(7829367, 1);
            plus.graphics.drawRect(4, 4, 2, 4);
            plus.graphics.drawRect(0, 8, 10, 2);
            plus.graphics.drawRect(4, 10, 2, 4);
            plus.graphics.endFill();
            plus.y = -5;
            fsBtn.addChild(plus);
            minus = new Sprite();
            minus.graphics.beginFill(7829367, 1);
            minus.graphics.drawRect(0, 8, 10, 2);
            minus.graphics.endFill();
            minus.y = -5;
            fsBtn.addChild(minus);
            lArrow = new Sprite();
            lArrow.graphics.beginFill(myRoot.hotColor, 0);
            lArrow.graphics.drawRect(-5, -10, 15, 30);
            lArrow.graphics.endFill();
            lArrow.graphics.beginFill(myRoot.coldColor, 100);
            lArrow.graphics.drawRect(0, 4, 1, 1);
            lArrow.graphics.drawRect(1, 3, 1, 3);
            lArrow.graphics.drawRect(2, 2, 1, 5);
            lArrow.graphics.drawRect(3, 1, 1, 7);
            lArrow.graphics.drawRect(4, 0, 1, 9);
            lArrow.graphics.endFill();
            lArrow.buttonMode = true;
            lArrow.name = "lArrow";
            lArrow.addEventListener(MouseEvent.MOUSE_OVER, arrowRollOver);
            lArrow.addEventListener(MouseEvent.MOUSE_OUT, arrowRollOut);
            lArrow.addEventListener(MouseEvent.CLICK, arrowClick);
            rArrow = new Sprite();
            rArrow.graphics.beginFill(myRoot.hotColor, 0);
            rArrow.graphics.drawRect(-5, -10, 15, 30);
            rArrow.graphics.endFill();
            rArrow.graphics.beginFill(myRoot.coldColor, 100);
            rArrow.graphics.drawRect(0, 0, 1, 9);
            rArrow.graphics.drawRect(1, 1, 1, 7);
            rArrow.graphics.drawRect(2, 2, 1, 5);
            rArrow.graphics.drawRect(3, 3, 1, 3);
            rArrow.graphics.drawRect(4, 4, 1, 1);
            rArrow.graphics.endFill();
            rArrow.buttonMode = true;
            rArrow.name = "rArrow";
            rArrow.addEventListener(MouseEvent.MOUSE_OVER, arrowRollOver);
            rArrow.addEventListener(MouseEvent.MOUSE_OUT, arrowRollOut);
            rArrow.addEventListener(MouseEvent.CLICK, arrowClick);
            if (myXMLList.length() > 1)
            {
                thebar.addChild(lArrow);
                thebar.addChild(rArrow);
            }
            fieldFormat = new TextFormat();
            fieldFormat.font = new HelveticaLightEmbed().fontName;
            fieldFormat.size = 14;
            fieldFormat.letterSpacing = 0;
            fieldFormat.leftMargin = 0;
            fieldFormat.rightMargin = 0;
            fieldFormat.color = myRoot.hotColor;
            btnField = new TextField();
            btnField.name = "btnField";
            btnField.autoSize = "left";
            btnField.antiAliasType = AntiAliasType.ADVANCED;
            btnField.gridFitType = GridFitType.SUBPIXEL;
            btnField.selectable = false;
            btnField.embedFonts = true;
            btnField.text = "";
            btnField.setTextFormat(fieldFormat);
            thebar.addChild(btnField);
            return;
        }

        function killImage()
        {
            if (image != null)
            {
                removeChild(image);
                image = null;
            }
            return;
        }

        function closeRollOut(event:MouseEvent) : void
        {
            return;
        }

        function buildImage() : void
        {
            image = Bitmap(loader.content);
            doResize();
            image.alpha = 0;
            addChild(image);
            if (myXMLList[currId].@border == "1")
            {
                frame = new Frame();
                frame.alpha = 0;
                addChild(frame);
                frame.resizeFrame(image.width, image.height);
                if (alpha == 1)
                {
                    TweenMax.to(frame, fadeSpeed, {alpha:1, overwrite:true});
                }
                else
                {
                    TweenMax.to(frame, fadeSpeed, {alpha:1, overwrite:true, delay:fadeSpeed});
                }
            }
            image.smoothing = true;
            if (alpha == 1)
            {
                TweenMax.to(image, fadeSpeed, {alpha:1, overwrite:true, onComplete:function ()
            {
                if (visible == true)
                {
                    image.smoothing = true;
                }
                return;
            }
            });
            }
            else
            {
                TweenMax.to(image, fadeSpeed, {alpha:1, overwrite:true, delay:fadeSpeed, onComplete:function ()
            {
                if (visible == true)
                {
                    image.smoothing = true;
                }
                return;
            }
            });
            }
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
            loader = null;
            return;
        }

        function onLoadComplete(event:Event) : void
        {
            var event:* = event;
            loaderbar.finishTween(fadeSpeed / 5);
            TweenMax.to(loaderbar, fadeSpeed, {alpha:0, overwrite:true});
            if (image == null)
            {
                buildImage();
            }
            else
            {
                if (frame != null)
                {
                    TweenMax.to(frame, fadeSpeed, {alpha:0, overwrite:true});
                }
                TweenMax.to(image, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                if (frame != null)
                {
                    removeChild(frame);
                    frame = null;
                }
                removeChild(image);
                image = null;
                buildImage();
                return;
            }
            });
            }
            return;
        }

        public function loadNextImage(param1:int) : void
        {
            loadImage(param1);
            return;
        }

        public function fadeInShell() : void
        {
            doResize();
            visible = true;
            thebar.alpha = 1;
            TweenMax.to(this, fadeSpeed, {alpha:1, overwrite:true});
            return;
        }

        function arrowClick(event:MouseEvent) : void
        {
            if (event.currentTarget.name == "closeBtn")
            {
                dispatchEvent(new ClickCloseEvent());
            }
            else if (event.currentTarget.name == "fsBtn")
            {
                if (plus.visible == false)
                {
                    plus.visible = true;
                    minus.visible = false;
                    myRoot.removeFromBookmarks(projId, myXMLList[currId].@id);
                    btnField.text = "Bookmark Image";
                }
                else
                {
                    plus.visible = false;
                    minus.visible = true;
                    myRoot.addToBookmarks(projId, myXMLList[currId].@id);
                    btnField.text = "Remove Bookmark";
                }
                btnField.setTextFormat(fieldFormat);
            }
            else if (event.currentTarget.name == "lArrow")
            {
                dispatchEvent(new ClickArrowEvent("prev"));
            }
            else if (event.currentTarget.name == "rArrow")
            {
                dispatchEvent(new ClickArrowEvent("next"));
            }
            return;
        }

    }
}
